package com.seven.ezpermissions

import android.content.Context
import android.content.Intent
import android.net.Uri
import android.provider.Settings
import androidx.annotation.StringRes
import androidx.appcompat.app.AlertDialog
import androidx.appcompat.app.AppCompatActivity
import androidx.fragment.app.Fragment

/**
 * 跳转App设置页Dialog
 * @author seven.xie
 * @date 2020/7/13 10:52
 * email: se7en.xie@qq.com
 */
class AppSettingsDialog private constructor(
    private val context: Context,
    private val title: String,
    private val rationale: String,
    private val positiveButton: String,
    private val negativeButton: String
) {

    fun show(): AlertDialog {
        return AlertDialog.Builder(context)
            .setTitle(title)
            .setMessage(rationale)
            .setPositiveButton(positiveButton) { dialog, _ ->
                dialog.dismiss()
                openAppSettingsUi(context)
            }
            .setNegativeButton(negativeButton) { dialog, _ ->
                dialog.dismiss()
            }
            .setCancelable(false)
            .show()
    }

    private fun openAppSettingsUi(context: Context) {
        val intent = Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS)
        intent.data = Uri.fromParts("package", context.packageName, null)
        context.startActivity(intent)
    }

    class Builder {

        private val context: Context

        private var title: String? = null
        private var rationale: String? = null
        private var positiveButton: String? = null
        private var negativeButton: String? = null

        constructor(activity: AppCompatActivity) {
            context = activity
        }

        constructor(fragment: Fragment) {
            context = fragment.context!!
        }

        fun setTitle(title: String?): Builder {
            this.title = title
            return this
        }

        fun setTitle(@StringRes title: Int): Builder {
            this.title = context.getString(title)
            return this
        }

        fun setRationale(rationale: String): Builder {
            this.rationale = rationale
            return this
        }

        fun setRationale(@StringRes rationale: Int): Builder {
            this.rationale = context.getString(rationale)
            return this
        }

        fun setPositiveButton(positiveButton: String): Builder {
            this.positiveButton = positiveButton
            return this
        }

        fun setPositiveButton(@StringRes positiveButton: Int): Builder {
            this.positiveButton = context.getString(positiveButton)
            return this
        }

        fun setNegativeButton(negativeButton: String): Builder {
            this.negativeButton = negativeButton
            return this
        }

        fun setNegativeButton(@StringRes negativeButton: Int): Builder {
            this.negativeButton = context.getString(negativeButton)
            return this
        }

        fun build(): AppSettingsDialog {
            if (title == null) {
                title = context.getString(R.string.ez_permission_title_settings_dialog)
            }
            if (rationale == null) {
                rationale = context.getString(R.string.ez_permissions_rationale_ask_again)
            }
            if (positiveButton == null) {
                positiveButton = context.getString(R.string.ez_permissions_ok)
            }
            if (negativeButton == null) {
                negativeButton = context.getString(R.string.ez_permissions_cancel)
            }
            return AppSettingsDialog(
                context,
                title ?: "",
                rationale ?: "",
                positiveButton ?: "",
                negativeButton ?: ""
            )
        }

        fun show(): AppSettingsDialog {
            val dialog = build()
            dialog.show()
            return dialog
        }
    }
}